package shop

import (
	"wxsdk/miniapp/core"
)

type SkuInfo struct {
	SkuId  string  `json:"sku_id"`
    Price  int     `json:"price"`
	Status int     `json:"status"`
}

type AddSkuProductList struct {
	ItemCode     string    `json:"item_code"`
    Title        string    `json:"title"`
	CategoryList []string  `json:"category_list"`
	ImageList    []string  `json:"image_list"`
	SrcWxappPath string    `json:"src_wxapp_path"`
	SkuInfos     *SkuInfo  `json:"sku_info,omitempty"`
}
type DelSkuProductList struct {
	ItemCode string `json:"item_code"`
    SkuId    string `json:"sku_id"`
}

// 导入“想买清单”
func AddShoppingList(accessToken,openId,itemId,skuId,categoryId,image,title string,price int) (err error) {
	request := struct {
	    UserOpenId      string            `json:"user_open_id"`
		SkuProductLists []*AddSkuProductList `json:"sku_product_list"`
	}{UserOpenId: openId}
	skuProductList := new(AddSkuProductList)
	skuProductList.ItemCode = itemId
	skuProductList.Title = title
	skuProductList.CategoryList = append(skuProductList.CategoryList, categoryId)
	skuProductList.ImageList = append(skuProductList.ImageList, image)
	skuProductList.SrcWxappPath = "/pages/goods/detail/index?item_id=" + itemId
	skuProductList.SkuInfos = new(SkuInfo)
	skuProductList.SkuInfos.SkuId = skuId
	skuProductList.SkuInfos.Price = price
	skuProductList.SkuInfos.Status = 1
	request.SkuProductLists = append(request.SkuProductLists, skuProductList)
	var result struct {
		core.Error
	}
	incompleteURL := "https://api.weixin.qq.com/mall/addshoppinglist?access_token="
	serverUrl := incompleteURL + accessToken
	client := core.GetPostJSONClient()
	if _, err = client.Excute("POST", serverUrl, request, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	return
}

// 删除“想买清单”
func DelShoppingList(accessToken,openId,itemId,skuId string) (err error) {
	request := struct {
	    UserOpenId      string `json:"user_open_id"`
		SkuProductLists []*DelSkuProductList `json:"sku_product_list"`
	}{UserOpenId: openId}
	request.SkuProductLists = append(request.SkuProductLists, &DelSkuProductList{ItemCode:itemId,SkuId:skuId})
	var result struct {
		core.Error
	}
	incompleteURL := "https://api.weixin.qq.com/mall/deleteshoppinglist?access_token="
	serverUrl := incompleteURL + accessToken
	client := core.GetPostJSONClient()
	if _, err = client.Excute("POST", serverUrl, request, &result); err != nil {
		return
	}
	if result.ErrCode != core.ErrCodeOK {
		err = &result.Error
		return
	}
	return
}
